package com.zxl.diy.weather.location;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

import com.zxl.manager.privacy.utils.ApplicationHelper;

/**
 * Created by Administrator on 2016/11/5.
 */
public class AndroidLocationHelper extends BaseLocationHelper implements LocationListener {

    private String mProvider;

    private LocationManager mLocationManager;

    private AndroidLocationHelper(String mProvider, Context mContext, OnLocationStatusListener mOnLocationStatusListener) {
        super(mContext, mOnLocationStatusListener);
        this.mProvider = mProvider;
        mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    }

    public static AndroidLocationHelper createNetLocationHelper(Context mContext, OnLocationStatusListener mOnLocationStatusListener) {
        return new AndroidLocationHelper(LocationManager.NETWORK_PROVIDER, mContext, mOnLocationStatusListener);
    }

    public static AndroidLocationHelper createGpsLocationHelper(Context mContext, OnLocationStatusListener mOnLocationStatusListener) {
        return new AndroidLocationHelper(LocationManager.GPS_PROVIDER, mContext, mOnLocationStatusListener);
    }

    @Override
    public void startLocation() {
        super.startLocation();
        if (mLocationManager.isProviderEnabled(mProvider)) {
            if (ActivityCompat.checkSelfPermission(ApplicationHelper.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(ApplicationHelper.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                mLocationManager.requestLocationUpdates(mProvider, 0, 0, this);
            } else {
                onLocationFailed(LOCATION_23_NO_PERMISSION);
            }
        } else {
            onLocationFailed(LOCATION_GPS_UNABLED);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        if (null != location) {
            super.onLocationSuccess(location);
        } else {
            super.onLocationFailed(LOCATION_NULL);
        }
        disablePhoneProvider();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        if (status == LocationProvider.OUT_OF_SERVICE) {
            disablePhoneProvider();
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
        if (mProvider.equals(provider)) {
            disablePhoneProvider();
            super.onLocationFailed(LOCATION_PROVIDER_UNABLED);
        }
    }

    private void disablePhoneProvider() {
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            mLocationManager.removeUpdates(this);
        }
    }
}
